import { ModalForm, ProForm, ProFormInstance, ProFormText } from '@ant-design/pro-components';
import { Form, message, Select } from 'antd';
import { useEffect, useRef } from 'react';
import { waitTime } from '@/utils/request';
import { addDepartment, updateDepartment } from '@/services/api/department';
import { addContact, updateContact } from '@/services/api/contact';

interface InputDialogProps {
  detailData?: API.ContactDTO;
  visible: boolean;
  onClose: (result: boolean) => void;
}

export default function InputDialog(props: InputDialogProps) {
  const form = useRef<ProFormInstance>(null);

  useEffect(() => {
    waitTime().then(() => {
      if (props.detailData) {
        form?.current?.setFieldsValue(props.detailData);
      } else {
        form?.current?.resetFields();
      }
    });
  }, [props.detailData, props.visible]);

  const onFinish = async (values: any) => {
    const { name, department, telephoneNumber, province, email, address } = values;
    const data: API.ContactDTO = {
      id: props.detailData?.id,
      name, 
      department, 
      telephoneNumber, 
      province, 
      email, 
      address
    };

    try{
      if (props.detailData) {
        await updateContact(data, { throwError: true});
      } else {
        await addContact(data, {throwError: true });
      }
    } catch (ex) {
      return true;
    }
    props.onClose(true);
    message.success('保存成功');
    return true;
  }

  //   let result;
  //   if (props.detailData) {
  //     result = await updateRecord(data);
  //   } else {
  //     result = await addRecord(data);
  //   }
  //   if (typeof result !== 'undefined'){
  //     message.success('保存成功');
  //   props.onClose(true);
  //   }
  //   return true;
  // };

  return (
    <ModalForm
      width={600}
      onFinish={onFinish}
      formRef={form}
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.onClose(false),
      }}
      title={props.detailData ? '修改联系人' : '新建联系人'}
      open={props.visible}
    >
      <ProFormText
        name="name"
        label="联系人名称"
        rules={[
          {
            required: true,
            message: '请输入联系人名称！',
          },
        ]}
      />
      <Form.Item label="省份" name="province" rules={[
          {
            required: true,
            message: '请选择联系人省份！',
          },
        ]}>
        <Select size='large'>
          <Select.Option value="北京">北京</Select.Option>
          <Select.Option value="天津">天津</Select.Option>
          <Select.Option value="上海">上海</Select.Option>
          <Select.Option value="重庆">重庆</Select.Option>
          <Select.Option value="河北">河北</Select.Option>
          <Select.Option value="山西">山西</Select.Option>
          <Select.Option value="辽宁">辽宁</Select.Option>
          <Select.Option value="吉林">吉林</Select.Option>
          <Select.Option value="黑龙江">黑龙江</Select.Option>
          <Select.Option value="江苏">江苏</Select.Option>
          <Select.Option value="浙江">浙江</Select.Option>
          <Select.Option value="安徽">安徽</Select.Option>
          <Select.Option value="福建">福建</Select.Option>
          <Select.Option value="江西">江西</Select.Option>
          <Select.Option value="山东">山东</Select.Option>
          <Select.Option value="河南">河南</Select.Option>
          <Select.Option value="湖北">湖北</Select.Option>
          <Select.Option value="湖南">湖南</Select.Option>
          <Select.Option value="广东">广东</Select.Option>
          <Select.Option value="海南">海南</Select.Option>
          <Select.Option value="贵州">贵州</Select.Option>
          <Select.Option value="云南">云南</Select.Option>
          <Select.Option value="陕西">陕西</Select.Option>
          <Select.Option value="甘肃">甘肃</Select.Option>
          <Select.Option value="青海">青海</Select.Option>
          <Select.Option value="台湾">台湾</Select.Option>
          <Select.Option value="内蒙古">内蒙古</Select.Option>
          <Select.Option value="广西">广西</Select.Option>
          <Select.Option value="西藏">西藏</Select.Option>
          <Select.Option value="宁夏">宁夏</Select.Option>
          <Select.Option value="新疆">新疆</Select.Option>
          <Select.Option value="香港">香港</Select.Option>
          <Select.Option value="澳门">澳门</Select.Option>
        </Select>
      </Form.Item>
      <ProForm.Group>
        <ProFormText
          name="department"
          label="单位"
          rules={[
            {
              required: true,
              message: '请输入单位名称！',
            },
          ]}
        />
        <ProFormText
          name="telephoneNumber"
          label="联系电话"
          rules={[
            {
              required: true,
              message: '请输入联系人电话！',
            },
          ]}
        />
        {/* <ProFormText
          name="province"
          label="省份"
          rules={[
            {
              required: true,
              message: '请输入联系人省份！',
            },
          ]}
        /> */}
        <ProFormText
          name="email"
          label="邮箱"
          rules={[
            {
              message: '请输入联系人邮箱！',
            },
          ]}
        />
        <ProFormText
          name="address"
          label="地址"
          rules={[
            {
              message: '请输入联系人地址！',
            },
          ]}
        />
      </ProForm.Group>
    </ModalForm>
  );
}
